﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 库房
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WarehouseController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        public WarehouseController(ITWarehouseRepository warehouseService, ILogRepository logRepository)
        {
            WarehouseService = warehouseService;
            this.logRepository = logRepository;


        }

        public ITWarehouseRepository WarehouseService { get; }

        /// <summary>
        /// 添加库房
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]

        public IActionResult AddWarehouse(TWarehouse m)
        {
            if(string.IsNullOrEmpty(m.Code)||m.Code.Length<=0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            ApiResult apiResult = new ApiResult();
            string uname = HttpContext.User.FindFirstValue("UserName");
            string runame = HttpContext.User.FindFirstValue("RealName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            m.CreateUserId = uid;
            m.CreateUserName = runame;
            string message = "";
            bool result = WarehouseService.AddWarehouse(m,out message);
            if (result == true)
            {
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了库房" + m.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                apiResult.code = "200";
                apiResult.message = "添加成功";
                return Ok(apiResult);
            }
            apiResult.code = "500";
            apiResult.message ="添加失败" +  message;
            return Ok(apiResult);
        }

        /// <summary>
        /// 修改库房
        /// </summary>
        /// <param name="wh"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdWarehouse(TWarehouse wh)
        {
            if (wh.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(wh.Code))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            string message;
            string uname = HttpContext.User.FindFirstValue("UserName");
            string runame = HttpContext.User.FindFirstValue("RealName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            wh.ModifyUserId = uid;
            wh.ModifyUserName = uname;
            var res = WarehouseService.UpdTWarehouse(wh,out message);
            if (res)
            {             
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了库房" + wh.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false,"修改失败"+ message, "500"));
        }
        /// <summary>
        /// 删除库房
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelWarehouse(int id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = WarehouseService.DelTWarehouse(id);
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了库房";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }
        /// <summary>
        /// 批量删除库房
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelWarehouseList(int[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (var id in idList)
            {
                var res = WarehouseService.DelTWarehouse(id);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了库房";
                //获取前端IP地址
                logg.Ipaddress = ip;
                logRepository.AddTLod(logg);
                if (!res)
                {
                    daRes = false;
                    break;
                }
            }
            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }
        /// <summary>
        /// 查询库房
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllTWarehouse(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var res = WarehouseService.GetAllTWarehouse(name, pageIndex, pageSize, out var totalCount);
            return Ok(BaseResult.Result(res, "200", totalCount));
        }
    }
}
